import { _decorator, Node, Color, Component, Sprite, SpriteFrame, UITransform, tween, v3 } from 'cc';
import LianLianKan_GameData from './LianLianKan_GameData';
import CommonFunction from 'db://assets/Script/Utils/CommonFunction';
import { bdlMgr } from 'db://assets/Script/Manager/BundleManager_S';
import { SSS_GameData } from 'db://assets/Script/Data/SSS_GameData';
const { ccclass, property } = _decorator;

@ccclass('LianLianKan_Block')
export class LianLianKan_Block extends Component {
    @property
    row: number = 0;

    @property
    col: number = 0;

    @property
    type: number = 0; // 图案类型

    @property({ type: Sprite })
    sprite: Sprite | null = null;

    @property({ type: Node })
    mask: Node | null = null;

    start() {
        this.node.getComponent(UITransform)?.setContentSize(LianLianKan_GameData.CellWidth, LianLianKan_GameData.CellHeight);
        // this.sprite = this.getComponent(Sprite);
    }


    // /** 根据类型刷新显示 */
    // refreshSprite() {
    //     if (this.spriteFrames && this.spriteFrames.length > 0) {
    //         this.sprite.spriteFrame = this.spriteFrames[this.type % this.spriteFrames.length];
    //     }
    // }

    // refreshSprite() {
    //     // 根据 this.type 改变 spriteFrame
    //     this.sprite.spriteFrame = this.getSpriteByType(this.type);
    // }

    select(highlight: boolean) {
        if (!this.sprite) return;
        if (highlight) {
            this.mask.active = true;
            // this.sprite.color = new Color(255, 200, 200); // 选中变红或偏色
        } else {
            this.mask.active = false;
            // this.sprite.color = new Color(255, 255, 255); // 恢复原色
        }
    }

    RefreshSprite() {
        // console.log("SSS_GameData.All_Sprite_List", SSS_GameData.All_Sprite_List);
        let list = SSS_GameData.All_Sprite_List;
        let item_data = list[this.type];

        bdlMgr.getRes<SpriteFrame>(`YangLeGeYang_Sprite`, `icon_e/${item_data.root}/${item_data.key}/spriteFrame`, SpriteFrame).then(res => {
            if (this.sprite) {
                this.sprite.spriteFrame = res;
                this.ResetSprite();
            }
        });
    }

    ResetSprite() {
        this.sprite.sizeMode = Sprite.SizeMode.TRIMMED;
        CommonFunction.SetFrame(this.sprite, LianLianKan_GameData.CellWidth - 14, LianLianKan_GameData.CellHeight - 14);
    }

    hintEffect() {
        // if (!this.sprite) return;
        tween(this.sprite.node)
            .to(0.30, { scale: v3(0.5, 0.5) })
            .to(0.30, { scale: v3(1.1, 1.1) })
            .union()    // 把前面的 to(0.2,0.8) + to(0.2,1) 合成一个 sequence
            .repeat(20)  // 重复 2 次
            .call(() => {
                this.sprite.node.scale = v3(1, 1);
            })
            .start();

        // tween(this.sprite)
        //     .to(0.2, { color: new Color(255, 255, 100) }) // 黄色闪一下
        //     .to(0.2, { color: new Color(255, 255, 255) })
        //     .union()
        //     .repeat(3)
        //     .start();
    }
}